// OptionsDialog.cpp

#include "StdAfx.h"

#include "App.h"
#include "Common/Lang/LangUtils.h"
#include "MyLoadMenu.h"
#include "Common/App/ProgramLocation.h"

#include "OptionsDialog.h"

#include "resource.h"

using namespace Common;


#ifndef UNDER_CE
typedef UINT32 (WINAPI * DllRegisterServerPtr)();

extern HWND g_MenuPageHWND;

static void ShowMenuErrorMessage(const wchar_t *m)
{
  MessageBoxW(g_MenuPageHWND, m, _T("7-Zip"), MB_ICONERROR);
}

static int DllRegisterServer2(const char *name)
{
  Common::System::DLL::CLibrary lib;

  CString prefix;
  App::GetProgramFolderPath(prefix);
  if (!lib.Load(prefix + _T("7-zip.dll")))
  {
    ShowMenuErrorMessage(_T("7-Zip cannot load 7-zip.dll"));
    return E_FAIL;
  }
  DllRegisterServerPtr f = (DllRegisterServerPtr)lib.GetProcAddress(name);
  if (f == NULL)
  {
    ShowMenuErrorMessage(_T("Incorrect plugin"));
    return E_FAIL;
  }
  HRESULT res = f();
  if (res != S_OK)
    ShowMenuErrorMessage(HResultToMessage(res));
  return (int)res;
}

STDAPI DllRegisterServer(void)
{
  #ifdef UNDER_CE
  return S_OK;
  #else
  return DllRegisterServer2("DllRegisterServer");
  #endif
}

STDAPI DllUnregisterServer(void)
{
  #ifdef UNDER_CE
  return S_OK;
  #else
  return DllRegisterServer2("DllUnregisterServer");
  #endif
}

#endif

void OptionsDialog(HWND hwndOwner)
{
  CString title = Lang::LangString(IDS_OPTIONS, 0x03010000);
  COptionsSheet dlg ((LPCTSTR)title, 0, hwndOwner);
  if(IDOK == dlg.DoModal(hwndOwner))
  {
	  if (dlg.m_pgLangPage.LangWasChanged)
    {
      g_App._window.SetWindowText(Lang::LangString(IDS_APP_TITLE, 0x03000000));
      MyLoadMenu();
    }
    g_App.SetListSettings();
    g_App.SetShowSystemMenu();
    g_App.RefreshAllPanels();
    // ::PostMessage(hwndOwner, kLangWasChangedMessage, 0 , 0);
  }
}

//////////////////////////////////////////////////////////////////////
// Construction

COptionsSheet::COptionsSheet ( _U_STRINGorID title, UINT uStartPage,
                               HWND hWndParent ) :
  baseClass ( title, uStartPage, hWndParent ),
  m_bCentered(false)
{
 m_psh.dwFlags |= PSH_NOAPPLYNOW | PSH_NOCONTEXTHELP;

   AddPage( m_pgSystemPage);
  // CPluginsPage pluginsPage;
  AddPage( m_pgEditPage);
  AddPage( m_pgSettingsPage);
  AddPage( m_pgLangPage);
  AddPage( m_pgMenuPage);
  AddPage( m_pgFoldersPage);
}


//////////////////////////////////////////////////////////////////////
// Message handlers

void COptionsSheet::OnShowWindow ( BOOL bShowing, int nReason )
{
    if ( bShowing && !m_bCentered )
        {
        m_bCentered = true;
        CenterWindow ( m_psh.hwndParent );
        }

  
	
}

BOOL COptionsSheet::OnInitDialog(CWindow wndFocus, LPARAM lInitParam)
{

  Lang::LangSetWindowText(m_pgSystemPage, 0x03010300);
  
  Lang::LangSetWindowText( m_pgEditPage, 0xFFFFFFFF);
  Lang::LangSetWindowText( m_pgSettingsPage, 0x01000200);
  Lang::LangSetWindowText( m_pgLangPage, 0x03010200);
  Lang::LangSetWindowText( m_pgMenuPage, 0x03010400);
  Lang::LangSetWindowText( m_pgFoldersPage, 0x01000400);
  return TRUE;
}